package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.RoomThingListAdapter;
import com.jiayang.hhotel.bean.CleanModel;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.RoomThingModel;
import com.jiayang.hhotel.bean.RoundsModel;
import com.jiayang.hhotel.presenter.RoomThingPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/18 0018.
 * 客房物品页面
 */

public class RoomThingActivity extends XActivity<RoomThingPresenter> implements View.OnClickListener, TitleListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    public static final String INTENT_ACTIVITY_FLAG="INTENT_ACTIVITY_FLAG";

    public static final int INTENT_FLAG_ROUNDS=100000;//查房页面跳转至此页面
    public static final int INTENT_FLAG_CLEAN=100001;//清洁页面跳转至此页面

    public static final String GUEST_THING_BACK_INTENT_KEY = "GUEST_THING_BACK_INTENT_KEY";//客房物品回退intent的key

    private ListView mListView;//列表
    private Button mGuestLoss;//客人损失按钮
    private Button mRoomCharge;//客房收费按钮

    private RoomThingListAdapter adapter;
    private RoundsModel.PCheckRoomListDtosBean checkRoomListDtosBean;
    private CleanModel.PCleanRoomListDtosBean cleanRoomListDtosBean;

    private int flag;//判断来自哪个页面的跳转

    private boolean isSel;//用户是否选择了数量

    private String roomNum;

    @Override
    public void initData(Bundle savedInstanceState) {

        initTitle(this,true,getString(R.string.room_thing),R.color.orange_color,getString(R.string.empty),this);
        flag = getIntent().getIntExtra(INTENT_ACTIVITY_FLAG,0);
        if(flag == INTENT_FLAG_ROUNDS){
            checkRoomListDtosBean = (RoundsModel.PCheckRoomListDtosBean) getIntent().getExtras().getSerializable("1");
            roomNum = checkRoomListDtosBean.getRoom();
        }else{
            cleanRoomListDtosBean = (CleanModel.PCleanRoomListDtosBean) getIntent().getExtras().getSerializable("1");
            roomNum = cleanRoomListDtosBean.getRoom();
        }

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        mListView= (ListView) findViewById(R.id.lv);
        mGuestLoss= (Button) findViewById(R.id.bt_guest_loss);
        mRoomCharge= (Button) findViewById(R.id.bt_room_charge);

        mGuestLoss.setOnClickListener(this);
        mRoomCharge.setOnClickListener(this);

        CommonUtil.showProgressDialog(this,R.string.loading);
        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,roomNum,3);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_guest_room_thing;
    }

    @Override
    public RoomThingPresenter newP() {
        return new RoomThingPresenter();
    }

    public void setAdapter(List<RoomThingModel.PHKTransListDtoBean> models){
        CommonUtil.dismissProgressDialog(this);
        isSuccess = true;
        if(adapter==null){
            adapter=new RoomThingListAdapter(this,models);
            mListView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    private boolean isSuccess;//判断请求列表是否成功
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        CommonUtil.dismissProgressDialog(this);
        isSuccess = false;
            mListView.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListView.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            CommonUtil.showProgressDialog(RoomThingActivity.this,R.string.loading);
                            getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE,roomNum,3);
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
    }

    private String str;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_guest_loss://客人遗失
               if(isSuccess){
                   str = setMpHKTrans();
                   if(!StringUtil.isEmpty(str)){
                       getP().asyncSave(Constant.LAUGUAGE_FLAG_CHINESE,0,roomNum,str,3);
                       isSel = true;
                   }else{
                       isSel = false;
                   }
               }
                break;
            case R.id.bt_room_charge://客房收费
                if(isSuccess){
                    str = setMpHKTrans();
                    if(!StringUtil.isEmpty(str)){
                        getP().asyncSave(Constant.LAUGUAGE_FLAG_CHINESE,1,roomNum,str,3);
                        isSel = true;
                    }else{
                        isSel = false;
                    }
                }
                break;
            case R.id.tv_back://回退按钮
                setResult();
                break;
        }
    }

    private void setResult(){
        Intent i = new Intent();
        if(isSel){//用户选择了数量
            i.putExtra(GUEST_THING_BACK_INTENT_KEY,1);
        }else{
            i.putExtra(GUEST_THING_BACK_INTENT_KEY,0);
        }
        RoomThingActivity.this.setResult(Constant.GUEST_ROOM_THING_RESULT_CODE,i);
        finish();
    }

    @Override
    public void onBackPressed() {
        setResult();
        super.onBackPressed();
    }

    //将选中的位置拼接成字符串
    private String setMpHKTrans(){
        String mpHKTrans="";
        HashMap<Integer,Integer> map=adapter.getCountMap();
        List<RoomThingModel.PHKTransListDtoBean> list=adapter.getmList();
        for(int i=0;i<map.size();i++){
            int count=map.get(i);
            if(count!=0){
                mpHKTrans+=list.get(i).getHKMenu()+","+count+",";
            }
        }
        if(!StringUtil.isEmpty(mpHKTrans)){
            mpHKTrans=mpHKTrans.substring(0,mpHKTrans.length()-1);
        }
        return mpHKTrans;
    }

    @Override
    public void rightClickListener() {
        if(adapter!=null){
            adapter.initMap();
        }
    }
}
